<?php
/**
 * CakeCMS - 后台AJAX操作
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
App::import ( 'Vendor', 'Taobaotop/TopClient' );
App::import ( 'Vendor', 'Taobaotop/RequestCheckUtil' );
App::import ( 'Vendor', 'Taobaotop/Logger' );
class AjaxController extends AppController {
	public $components = array (
			'CheckUserPermission' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		// Ajax里不使用模板
		$this->autoRender = false;
	}
	public function index() {
	}
	
	/**
	 * 当前位置
	 */
	public function pos() {
		$mid = $this->data ['mid'];
		
		$this->loadModel ( "Menu" );
		echo $this->Menu->current_pos ( $mid );
		exit ();
	}
	
	/**
	 * 设置基础分类
	 */
	public function getBasic() {
		$brand_cate = $this->data ['brand_cate'];
		
		if (empty ( $brand_cate ) || $brand_cate == 0) {
			echo "<option value=\"0\">请选择上级分类</option>";
			exit ();
		} else {
			$this->loadModel ( "BrandCate" );
			$conditions = array (
					"parentid" => $brand_cate 
			);
			
			$BrandList = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			if ($BrandList) {
				$html = "<option value=\"0\">请选择上级分类</option>";
				
				foreach ( $BrandList as $val ) {
					$is_selected = "";
					$html .= "<option {$is_selected} value=\"{$val['BrandCate']['id']}\">{$val['BrandCate']['name']}</option>";
				}
				echo $html;
				exit ();
			} else {
				echo "<option value=\"0\">请选择上级分类</option>";
				exit ();
			}
		}
	}
	
	/**
	 * 设置基础分类
	 */
	public function getBrand() {
		$basic_cate = $this->data ['basic_cate'];
		
		if (empty ( $basic_cate ) || $basic_cate == 0) {
			echo "<option value=\"0\">请选择上级分类</option>";
			exit ();
		} else {
			$this->loadModel ( "BrandCate" );
			$conditions = array (
					"parentid" => $basic_cate 
			);
			
			$BrandList = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			if ($BrandList) {
				$html = "<option value=\"0\">请选择上级分类</option>";
				
				foreach ( $BrandList as $val ) {
					$is_selected = "";
					$html .= "<option {$is_selected} value=\"{$val['BrandCate']['id']}\">{$val['BrandCate']['name']}</option>";
				}
				echo $html;
				exit ();
			} else {
				echo "<option value=\"0\">请选择上级分类</option>";
				exit ();
			}
		}
	}
	
	/**
	 * 获取淘宝url地址id
	 *
	 * @param string $url        	
	 * @return Ambigous <number, unknown>
	 */
	private function get_id($url) {
		$id = 0;
		$parse = parse_url ( $url );
		if (isset ( $parse ['query'] )) {
			parse_str ( $parse ['query'], $params );
			if (isset ( $params ['id'] )) {
				$id = $params ['id'];
			} elseif (isset ( $params ['item_id'] )) {
				$id = $params ['item_id'];
			} elseif (isset ( $params ['default_item_id'] )) {
				$id = $params ['default_item_id'];
			}
		}
		return $id;
	}
	
	/**
	 * 淘宝获取数据
	 */
	public function getTaobao() {
		$url = $this->data ['url'];
		// 获取地址url的ID
		$id = $this->get_id ( $this->data ['url'] );
		
		// 淘宝客PID
		$taobao_app_pid = Configure::read ( 'admin_config.taobao_app_pid' );
		$taobao_app_key = Configure::read ( 'admin_config.taobao_app_key' );
		$taobao_app_secret = Configure::read ( 'admin_config.taobao_app_secret' );
		
		// 这部份是取从taobao获取商品信息
		$tb_top = new TopClient ();
		$tb_top->appkey = $taobao_app_key;
		$tb_top->secretKey = $taobao_app_secret;
		
		$req = $tb_top->load_api ( 'ItemGetRequest' );
		$req->setFields ( "detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual" );
		$req->setNumIid ( $id );
		$resp = $tb_top->execute ( $req );
		$res = ( array ) $resp;
		
		echo json_encode ( $res );
		exit ();
	}
	
	/**
	 * 右下角提示消息
	 */
	public function message() {
	}
	
	/**
	 * 锁屏
	 */
	public function screenlock() {
		$this->Session->write ( 'lock_screen', 1 );
	}
	
	/**
	 * 解除锁屏
	 */
	public function loginscreenlock() {
		$lock_password = $this->data ['lock_password'];
		
		if (empty ( $lock_password )) {
			echo '2';
			exit ();
		}
		
		$this->loadModel ( 'Admin' );
		$AdminInfo = $this->Admin->GetAdminByID ( $this->Session->read ( 'admin_uid' ) );
		if (md5 ( $lock_password ) != $AdminInfo ['Admin'] ['password']) {
			echo '3';
			exit ();
		} else {
			$this->Session->write ( 'lock_screen', 0 );
			echo "1";
			exit ();
		}
	}
}
?>